package com.txf.coomon.ui.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;

import com.google.gson.JsonElement;
import com.txf.coomon.R;
import com.txf.coomon.ben.DemoItem;
import com.txf.coomon.mvp.net_okhttp.contract.DemoContract;
import com.txf.coomon.mvp.net_okhttp.presenter.DemoPresenter;
import com.txf.coomon.ui.fragment.OkHttpFragment;
import com.txf.coomon.ui.view.SimpleNavigationBar;
import com.txf.other_toolslibrary.utils.ToastUtils;
import com.txf.ui_mvplibrary.interfaces.INavigationBar;
import com.txf.ui_mvplibrary.interfaces.ITitleBar;
import com.txf.ui_mvplibrary.interfaces.OnAppListener;
import com.txf.ui_mvplibrary.ui.activity.BaseMVPRecyclerViewActivity;
import com.txf.ui_mvplibrary.ui.adapter.BaseCompleteRecyclerAdapter;
import com.txf.ui_mvplibrary.ui.adapter.BaseRecyclerAdapter;
import com.txf.ui_mvplibrary.ui.adapter.hoder.BaseViewHoder;
import com.txf.ui_mvplibrary.ui.view.deft.DefaultTitleBar;

import java.util.ArrayList;
import java.util.List;

public class MainOkHttpActivity
        extends BaseMVPRecyclerViewActivity<DemoItem, DemoContract.View, DemoContract.Presenter>
        implements DemoContract.View, OnAppListener.OnAdapterListener {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContract(this, new DemoPresenter(this));
        mITitleBar.setTitleText("首页");
        mINavigationBar.getView().setBackgroundColor(0XFF2577E3);
    }

    @Override
    protected BaseRecyclerAdapter<DemoItem> buildAdapter() {
        return new BaseCompleteRecyclerAdapter<DemoItem>(this, this) {
            @Override
            protected void setItemLayout() {
                super.setItemLayout();
                addItemLayout(0, R.layout.item_view_simple);
            }

            @Override
            protected void onBindBaseViewHoder(BaseViewHoder holder, int position, final DemoItem item) {
                String value = (String) item.getData();
                holder.setText(R.id.value, value);
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        getListener().onInteractionAdapter(item.getAction(), null);
                    }
                });
            }
        };
    }

    @Override
    protected void request(int pageIndex, int pageSize) {
//        HashMap<String, Object> maps = new HashMap<>();
//        maps.put("page", "1");
//        maps.put("offset", "10");
//        getPresenter().getData(this, 0, maps);
        mRecyclerView.postDelayed(new Runnable() {
            @Override
            public void run() {
                testItemData();
            }
        }, 1000);
    }

    private void testItemData() {
        List<DemoItem> items = new ArrayList<>();
        items.add(new DemoItem("显示Loading", 0, 0));
        items.add(new DemoItem("显示弹窗", 0, 1));
        items.add(new DemoItem("刷新页面", 0, 2));
        items.add(new DemoItem("显示Fragment", 0, 3));
        items.add(new DemoItem("隐藏Fragment", 0, 4));
        items.add(new DemoItem("删除Fragment", 0, 5));
        refreshData(items);
    }

    @Override
    public void httpOnError(int with, int error, String message) {

    }

    @Override
    public void httpOnSuccess(int with, JsonElement data, String message) {

    }

    @Override
    public void onInteractionAdapter(int action, Bundle bundle) {
        switch (action) {
            case 0:
                showLoading();
                break;
            case 1:
                new Builder()
                        .setWith(1)
                        .setTitle("标题")
                        .setContent("内容内容内容内容内容内容")
                        .setButton1("按钮1")
                        .setButton2("按钮2")
                        .setButton3("按钮3")
                        .show();
                break;
            case 2:
                startRequest(pageIndex, pageSize);
                break;
            case 3:
                addFragment(R.id.frameLayout, new OkHttpFragment(), TAG_OKHTTPFRAGMENT);
                break;
            case 4:
                hideFragment(TAG_OKHTTPFRAGMENT);
                break;
            case 5:
                removeFragment(TAG_OKHTTPFRAGMENT);
                break;
        }
    }

    private final static String TAG_OKHTTPFRAGMENT = "OkHttpFragment";

    @Override
    protected void onInteractionDialogClick(DialogInterface dialog, int which, int with, Bundle ext) {
        super.onInteractionDialogClick(dialog, which, with, ext);
        if (with == 1) {
            switch (which) {
                case DialogInterface.BUTTON_NEUTRAL:
                    ToastUtils.show("按钮1");
                    break;
                case DialogInterface.BUTTON_NEGATIVE:
                    ToastUtils.show("按钮2");
                    break;
                case DialogInterface.BUTTON_POSITIVE:
                    ToastUtils.show("按钮3");
                    break;
            }
        }
    }

    @Override
    protected ITitleBar buildTitleBar() {
        return new DefaultTitleBar(this);
    }

    @Override
    protected INavigationBar buildNavigationBar() {
        return new SimpleNavigationBar(this);
    }
}
